%23%20%2F%2F%2F%20script%0A%23%20requires-python%20%3D%20%22%3E%3D3.13%22%0A%23%20dependencies%20%3D%20%5B%0A%23%20%20%20%20%20%22kaleido%3D%3D1.2.0%22%2C%0A%23%20%20%20%20%20%22marimo%22%2C%0A%23%20%20%20%20%20%22numpy%3D%3D2.4.2%22%2C%0A%23%20%20%20%20%20%22pandas%3D%3D3.0.1%22%2C%0A%23%20%20%20%20%20%22plotly%3D%3D6.5.2%22%2C%0A%23%20%20%20%20%20%22scipy%3D%3D1.17.0%22%2C%0A%23%20%20%20%20%20%22statsmodels%3D%3D0.14.6%22%2C%0A%23%20%5D%0A%23%20%2F%2F%2F%0A%0Aimport%20marimo%0A%0A__generated_with%20%3D%20%220.20.1%22%0Aapp%20%3D%20marimo.App(width%3D%22medium%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.hstack(%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20mo.image(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20src%3D%22http%3A%2F%2Fmodernmacro.org%2Fresources%2FVivaldo%2Ffigures%2FOldTests%2FRIR.png%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20width%3D200%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mo.image(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20src%3D%22http%3A%2F%2Fmodernmacro.org%2Fresources%2FVivaldo%2Ffigures%2Fiscte.png%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20width%3D300%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20align%3D%22center%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%3Cstyle%3E%0A%20%20%20%20.marimo-cell%20%7B%0A%20%20%20%20%20%20%20%20border%3A%20none%20!important%3B%0A%20%20%20%20%20%20%20%20box-shadow%3A%20none%20!important%3B%0A%20%20%20%20%20%20%20%20margin%3A%200%20!important%3B%0A%20%20%20%20%20%20%20%20padding%3A%200%20!important%3B%0A%20%20%20%20%7D%0A%0A%20%20%20%20.marimo-cell-content%20%7B%0A%20%20%20%20%20%20%20%20padding%3A%200.5rem%200%20!important%3B%0A%20%20%20%20%7D%0A%20%20%20%20.admonition%20%7B%0A%20%20%20%20margin-top%3A%20-0.99rem%20!important%3B%0A%20%20%20%20margin-bottom%3A%20-0.99rem%20!important%3B%0A%20%20%20%20%7D%0A%20%20%20%20h1%20%7B%0A%20%20%20%20%20%20%20%20margin-bottom%3A%20-1.2rem%20!important%3B%0A%20%20%20%20%7D%0A%20%20%20%20h2%20%7B%0A%20%20%20%20%20%20%20%20margin-bottom%3A%20-1.2rem%20!important%3B%0A%20%20%20%20%7D%0A%20%20%20%20%2F*%20Reduce%20spacing%20before%20bullet%20lists%20*%2F%0A%20%20%20%20ul%20%7B%0A%20%20%20%20%20%20%20%20margin-top%3A%20-0.3rem%20!important%3B%0A%20%20%20%20%7D%0A%20%20%20%20%2F*%20Optionally%20reduce%20spacing%20between%20list%20items%20*%2F%0A%20%20%20%20li%20%7B%0A%20%20%20%20%20%20%20%20margin-bottom%3A%200.2rem%20!important%3B%0A%20%20%20%20%7D%0A%20%20%20%20%2F*%20Increase%20font%20size%20for%20all%20text%20in%20the%20document%20*%2F%0A%20%20%20%20body%20%7B%0A%20%20%20%20%20%20%20%20font-size%3A%2016px%3B%0A%20%20%20%20%7D%0A%0A%20%20%20%20%2F*%20Increase%20font%20size%20for%20all%20headers%20*%2F%0A%20%20%20%20h1%20%7B%0A%20%20%20%20%20%20%20%20font-size%3A%2036px%20!important%3B%0A%20%20%20%20%7D%0A%0A%20%20%20%20h2%20%7B%0A%20%20%20%20%20%20%20%20font-size%3A%2028px%20!important%3B%0A%20%20%20%20%7D%0A%0A%20%20%20%20h3%20%7B%0A%20%20%20%20%20%20%20%20font-size%3A%2025px%20!important%3B%0A%20%20%20%20%7D%0A%20%20%20%20%3C%2Fstyle%3E%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20The%20HP%20filter%20%26%20Impulse%20Response%20Functions%0A%20%20%20%20%3Cbr%3E%0A%20%20%20%20**February%2023%2C%202026**%0A%0A%20%20%20%20**Vivaldo%20Mendes**%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20---%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20%23%23%23%20Packages%20used%20in%20this%20notebook%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20import%20pandas%20as%20pd%0A%20%20%20%20from%20scipy.optimize%20import%20fsolve%20%0A%20%20%20%20from%20scipy.sparse.linalg%20import%20spsolve%0A%20%20%20%20from%20scipy.sparse%20import%20diags%0A%20%20%20%20import%20plotly.graph_objects%20as%20go%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20from%20plotly.subplots%20import%20make_subplots%0A%20%20%20%20import%20plotly.express%20as%20px%0A%20%20%20%20import%20plotly.io%20as%20pio%20%0A%20%20%20%20pio.templates.default%20%3D%20%22plotly%22%0A%20%20%20%20import%20kaleido%0A%20%20%20%20pio.defaults.default_format%20%3D%20%22svg%22%0A%20%20%20%20from%20datetime%20import%20date%2C%20datetime%0A%20%20%20%20from%20numpy.linalg%20import%20eigvals%2C%20inv%0A%20%20%20%20from%20statsmodels.tsa.stattools%20import%20acf%2C%20ccf%20as%20statsmodels_ccf%0A%20%20%20%20import%20warnings%0A%20%20%20%20warnings.filterwarnings(%22ignore%22)%0A%20%20%20%20import%20os%0A%20%20%20%20os.chdir(os.path.dirname(__file__))%0A%20%20%20%20return%20diags%2C%20go%2C%20mo%2C%20np%2C%20pd%2C%20spsolve%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20import%20os%0A%20%20%20%20%23os.getcwd()%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20%23%23%201.%20Functions%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%2F%2F%2F%20attention%0A%0A%20%20%20%20Students%20are%20not%20expected%20to%20understand%20all%20the%20small%20details%20of%20the%20elements%20that%20comprise%20the%20functions%20in%20this%20section%2C%20but%20they%20do%20need%20to%20know%20how%20to%20use%20them%20in%20the%20sections%20below.%20Students%20do%20not%20need%20to%20study%20these%20functions%3B%20however%2C%20they%20need%20to%20understand%20very%20well%20what%20they%20do%20(what%20they%20are%20used%20for).%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20The%20functions%20below%20will%20be%20used%20in%20this%20notebook%20to%20compute%20the%20Hodrick-Prescott%20trend%20(HP%20trend).%20The%20only%20indispensable%20function%20is%20%3Ckbd%3Ehp_trend%3C%2Fkbd%3E.%20But%20to%20make%20our%20lives%20easier%2C%20we%20also%20wrote%20%3Ckbd%3Ehp_trend_df%3C%2Fkbd%3E%2C%20which%20allows%20us%20to%20apply%20the%20former%20to%20an%20entire%20dataframe.%20The%20final%20function%20%3Ckbd%3Edf_arith_operations%3C%2Fkbd%3E%20is%20used%20to%20implement%20simple%20arithmetic%20operations%20over%20two%20dataframes%20(like%20%3Ckbd%3E%2B%3C%2Fkbd%3E%20%2C%20%3Ckbd%3E-%3C%2Fkbd%3E%20%2C%20%3Ckbd%3E*%3C%2Fkbd%3E%20%2C%20%3Ckbd%3E%5C%3C%2Fkbd%3E%20%2C%20etc.)%2C%20by%20skipping%20the%20column%20with%20the%20dates.%0A%0A%20%20%20%20A%20complete%20understanding%20of%20the%20code%20in%20these%20three%20functions%20will%20not%20be%20required%20in%20this%20course.%20But%20students%20should%20understand%20how%20(and%20why)%20we%20will%20use%20them%20in%20this%20notebook.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(diags%2C%20np%2C%20spsolve)%3A%0A%20%20%20%20def%20hp_trend(mydata%3A%20np.ndarray%2C%20%CE%BB%3A%20float)%20-%3E%20np.ndarray%3A%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20Computes%20the%20Hodrick-Prescott%20trend%20trend.%0A%0A%20%20%20%20%20%20%20%20Builds%20the%20symmetric%20band%20matrix%20A%20from%20the%20first-order%20conditions%0A%20%20%20%20%20%20%20%20of%20the%20HP%20minimization%20problem%20using%20sparse%20diagonals%3A%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20A%20%3D%20diags(%5Bdiag2%2C%20diag1%2C%20diag0%2C%20diag1%2C%20diag2%5D%2C%20%5B-2%2C-1%2C0%2C1%2C2%5D)%0A%0A%20%20%20%20%20%20%20%20Parameters%0A%20%20%20%20%20%20%20%20----------%0A%20%20%20%20%20%20%20%20mydata%20%3A%201-D%20numpy%20array%20of%20float64%0A%20%20%20%20%20%20%20%20%CE%BB%20%20%20%20%20%20%3A%20smoothing%20parameter%20(e.g.%201600%20for%20quarterly%20data)%0A%0A%20%20%20%20%20%20%20%20Returns%0A%20%20%20%20%20%20%20%20-------%0A%20%20%20%20%20%20%20%20trend%20%3A%201-D%20numpy%20array%20(the%20HP%20trend%20%CF%84)%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20n%20%3D%20len(mydata)%0A%20%20%20%20%20%20%20%20assert%20n%20%3E%3D%204%2C%20%22Need%20at%20least%204%20observations.%22%0A%0A%20%20%20%20%20%20%20%20diag2%20%3D%20%CE%BB%20*%20np.ones(n%20-%202)%0A%20%20%20%20%20%20%20%20diag1%20%3D%20np.concatenate(%5B%5B-2%20*%20%CE%BB%5D%2C%20-4%20*%20%CE%BB%20*%20np.ones(n%20-%203)%2C%20%5B-2%20*%20%CE%BB%5D%5D)%0A%20%20%20%20%20%20%20%20diag0%20%3D%20np.concatenate(%0A%20%20%20%20%20%20%20%20%20%20%20%20%5B%5B1%20%2B%20%CE%BB%5D%2C%20%5B1%20%2B%205%20*%20%CE%BB%5D%2C%20(1%20%2B%206%20*%20%CE%BB)%20*%20np.ones(n%20-%204)%2C%20%5B1%20%2B%205%20*%20%CE%BB%5D%2C%20%5B1%20%2B%20%CE%BB%5D%5D%0A%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20%20%20%20%20A%20%3D%20diags(%5Bdiag2%2C%20diag1%2C%20diag0%2C%20diag1%2C%20diag2%5D%2C%20%5B-2%2C%20-1%2C%200%2C%201%2C%202%5D%2C%20format%3D%22csc%22)%0A%0A%20%20%20%20%20%20%20%20return%20spsolve(A%2C%20mydata)%0A%0A%20%20%20%20return%20(hp_trend%2C)%0A%0A%0A%40app.cell%0Adef%20_(hp_trend%2C%20pd)%3A%0A%20%20%20%20def%20hp_trend_df(df%3A%20pd.DataFrame%2C%20%CE%BB%3A%20float)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20Applies%20hp_trend%20to%20every%20numeric%20column%20of%20a%20DataFrame%2C%0A%20%20%20%20%20%20%20%20preserving%20the%20first%20(date)%20column%20unchanged.%0A%0A%20%20%20%20%20%20%20%20Parameters%0A%20%20%20%20%20%20%20%20----------%0A%20%20%20%20%20%20%20%20df%20%3A%20DataFrame%20whose%20first%20column%20is%20a%20date%2Fperiod%20object%0A%20%20%20%20%20%20%20%20%CE%BB%20%20%3A%20HP%20smoothing%20parameter%0A%0A%20%20%20%20%20%20%20%20Returns%0A%20%20%20%20%20%20%20%20-------%0A%20%20%20%20%20%20%20%20trended_df%20%3A%20DataFrame%20with%20trend%20values%20(same%20column%20names)%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20colnames%20%3D%20df.columns.tolist()%0A%20%20%20%20%20%20%20%20trended_df%20%3D%20pd.DataFrame()%0A%20%20%20%20%20%20%20%20trended_df%5Bcolnames%5B0%5D%5D%20%3D%20df%5Bcolnames%5B0%5D%5D%0A%0A%20%20%20%20%20%20%20%20for%20col%20in%20colnames%5B1%3A%5D%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20trended_df%5Bcol%5D%20%3D%20hp_trend(df%5Bcol%5D.values.astype(float)%2C%20%CE%BB)%0A%0A%20%20%20%20%20%20%20%20return%20trended_df%0A%0A%20%20%20%20return%20(hp_trend_df%2C)%0A%0A%0A%40app.cell%0Adef%20_(pd)%3A%0A%20%20%20%20def%20df_arith_operations(df1%3A%20pd.DataFrame%2C%20df2%3A%20pd.DataFrame%2C%20op)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20Applies%20a%20binary%20operation%20column-wise%20between%20two%20DataFrames%2C%0A%20%20%20%20%20%20%20%20skipping%20the%20first%20(date)%20column.%0A%0A%20%20%20%20%20%20%20%20Example%20%E2%80%94%20cycle%20%3D%20original%20%E2%88%92%20trend%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20USdata_cycles%20%3D%20df_arith_operations(USdata%2C%20USdata_trends%2C%20np.subtract)%0A%0A%20%20%20%20%20%20%20%20Parameters%0A%20%20%20%20%20%20%20%20----------%0A%20%20%20%20%20%20%20%20df1%2C%20df2%20%3A%20DataFrames%20with%20identical%20column%20names%0A%20%20%20%20%20%20%20%20op%20%20%20%20%20%20%20%3A%20a%20numpy%20ufunc%20or%20any%20callable%20f(a%2C%20b)%20%E2%86%92%20array%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20e.g.%20np.subtract%2C%20np.add%2C%20np.divide%2C%20np.multiply%0A%0A%20%20%20%20%20%20%20%20Returns%0A%20%20%20%20%20%20%20%20-------%0A%20%20%20%20%20%20%20%20result_df%20%3A%20DataFrame%20with%20the%20operation%20applied%20column-wise%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20colnames%20%3D%20df1.columns.tolist()%0A%20%20%20%20%20%20%20%20result_df%20%3D%20pd.DataFrame()%0A%20%20%20%20%20%20%20%20result_df%5Bcolnames%5B0%5D%5D%20%3D%20df1%5Bcolnames%5B0%5D%5D%0A%0A%20%20%20%20%20%20%20%20for%20col%20in%20colnames%5B1%3A%5D%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20result_df%5Bcol%5D%20%3D%20op(df1%5Bcol%5D.values%2C%20df2%5Bcol%5D.values)%0A%0A%20%20%20%20%20%20%20%20return%20result_df%0A%0A%20%20%20%20return%20(df_arith_operations%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20%23%23%202.%20Data%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20%23%23%23%20Read%20the%20data%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20To%20read%20or%20load%20the%20data%20into%20the%20notebook%2C%20we%20use%20%3Ckbd%3Epd.read_csv%3C%2Fkbd%3E.%20The%20CSV%20file%20%3Ckbd%3EData_US.csv%3C%2Fkbd%3E%20must%20be%20located%20in%20the%20same%20folder%20as%20this%20notebook%3A%0A%0A%20%20%20%20%60%60%60python%0A%20%20%20%20%20%20%20%20%20%20%20%20USdata%20%3D%20pd.read_csv(%22Data_US.csv%22%2C%20parse_dates%3D%5B%22Quarters%22%5D)%0A%20%20%20%20%60%60%60%0A%20%20%20%20Usually%2C%20the%20%3Ckbd%3Epandas%3C%2Fkbd%3E%20library%20is%20capable%20of%20inferring%20the%20correct%20date%20object%20(like%20quarters%20in%20the%20current%20case)%20and%2C%20therefore%2C%20to%20load%20the%20data%20set%20it%20will%20be%20enough%20to%20type%20the%20following%20code%3A%0A%0A%20%20%20%20%60%60%60python%0A%20%20%20%20%20%20%20%20%20%20%20%20USdata%20%3D%20pd.read_csv(%22Data_US.csv%22)%0A%20%20%20%20%60%60%60%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(pd%2C%20preview)%3A%0A%20%20%20%20USdata%20%3D%20pd.read_csv(%22Data_US.csv%22)%20%20%23%20%2C%20parse_dates%3D%5B%22Quarters%22%5D)%0A%20%20%20%20preview(USdata)%0A%20%20%20%20return%20(USdata%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20---%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20%23%23%23%20Create%20sub-samples%20of%20your%20data%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20To%20work%20with%20a%20particular%20sub-period%2C%20stipulate%20an%20initial%20and%20an%20end%20date%3A%0A%0A%20%20%20%20%60%60%60python%0A%20%20%20%20my_new_data%20%3D%20USdata%5B%20(USdata%5B%22Quarters%22%5D%20%3E%3D%20%221960-01-01%22)%20%26%20(USdata%5B%22Quarters%22%5D%20%3C%3D%20%221961-10-01%22)%20%5D%0A%20%20%20%20%60%60%60%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(USdata)%3A%0A%20%20%20%20my_new_data%20%3D%20USdata%5B%20(USdata%5B%22Quarters%22%5D%20%3E%3D%20%221960-01-01%22)%20%26%20(USdata%5B%22Quarters%22%5D%20%3C%3D%20%221961-10-01%22)%20%5D%0A%20%20%20%20my_new_data%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20---%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20%23%23%23%20Doing%20some%20plotting%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(USdata%2C%20go)%3A%0A%20%20%20%20_fig%20%3D%20go.Figure()%0A%20%20%20%20_fig.add_trace(%0A%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3DUSdata%5B%22Quarters%22%5D%2C%20y%3DUSdata%5B%22CPI%22%5D%2C%20mode%3D%22lines%22%2C%20name%3D%22Inflation%20(%CF%80)%22%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20)%0A%20%20%20%20_fig.add_trace(%0A%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3DUSdata%5B%22Quarters%22%5D%2C%20y%3DUSdata%5B%22UR%22%5D%2C%20mode%3D%22lines%22%2C%20name%3D%22Unemployment%20(u)%22%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20)%0A%20%20%20%20_fig.update_layout(%0A%20%20%20%20%20%20%20%20height%3D460%2C%0A%20%20%20%20%20%20%20%20title_text%3D%22Unemployment%20and%20inflation%20in%20the%20US%3A%201960Q1%E2%80%932023Q4%22%2C%0A%20%20%20%20%20%20%20%20title_x%3D0.5%2C%0A%20%20%20%20%20%20%20%20hovermode%3D%22x%22%2C%0A%20%20%20%20%20%20%20%20font_family%3D%22Calibri%22%2C%0A%20%20%20%20%20%20%20%20font_size%3D14%2C%0A%20%20%20%20%20%20%20%20legend%3Ddict(x%3D0.03%2C%20y%3D0.95%2C%20bgcolor%3D%22rgba(0%2C0%2C0%2C0)%22)%2C%0A%20%20%20%20%20%20%20%20xaxis_range%3D%5B%221959-04-01%22%2C%20%222024-10-01%22%5D%2C%0A%20%20%20%20%20%20%20%20margin%3Ddict(l%3D70%2C%20r%3D60%2C%20t%3D70%2C%20b%3D60)%2C%0A%20%20%20%20)%0A%20%20%20%20_fig%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(USdata%2C%20go)%3A%0A%20%20%20%20_fig2%20%3D%20go.Figure()%0A%0A%20%20%20%20_fig2.add_trace(%0A%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3DUSdata%5B%22Quarters%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3DUSdata%5B%22CPI%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22lines%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Inflation%20(%CF%80)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(color%3D%22blueviolet%22%2C%20width%3D2)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20yaxis%3D%22y2%22%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20)%0A%20%20%20%20_fig2.add_trace(%0A%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3DUSdata%5B%22Quarters%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3DUSdata%5B%22UR%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22lines%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Unemployment%20(u)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(color%3D%22darkblue%22%2C%20width%3D2)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20yaxis%3D%22y2%22%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20)%0A%20%20%20%20_fig2.add_trace(%0A%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3DUSdata%5B%22Quarters%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3DUSdata%5B%22GDP%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22lines%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22GDP%20(y)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(color%3D%22maroon%22%2C%20width%3D2)%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20)%0A%0A%20%20%20%20_fig2.update_layout(%0A%20%20%20%20%20%20%20%20margin%3Ddict(l%3D70%2C%20r%3D60%2C%20t%3D70%2C%20b%3D60)%2C%0A%20%20%20%20%20%20%20%20height%3D460%2C%0A%20%20%20%20%20%20%20%20title_text%3D%22GDP%2C%20Unemployment%20and%20Inflation%20in%20the%20US%3A%201960Q1%E2%80%932023Q4%22%2C%0A%20%20%20%20%20%20%20%20title_x%3D0.5%2C%0A%20%20%20%20%20%20%20%20hovermode%3D%22x%22%2C%0A%20%20%20%20%20%20%20%20font_family%3D%22Calibri%22%2C%0A%20%20%20%20%20%20%20%20font_size%3D14%2C%0A%20%20%20%20%20%20%20%20legend%3Ddict(x%3D0.02%2C%20y%3D0.94%2C%20bgcolor%3D%22rgba(0%2C0%2C0%2C0)%22)%2C%0A%20%20%20%20%20%20%20%20xaxis%3Ddict(%0A%20%20%20%20%20%20%20%20%20%20%20%20title%3D%22Quarterly%20observations%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20showgrid%3DTrue%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20range%3D%5B%221959-04-01%22%2C%20%222024-10-01%22%5D%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20yaxis%3Ddict(%0A%20%20%20%20%20%20%20%20%20%20%20%20title%3Ddict(text%3D%22Real%20GDP%22%2C%20font%3Ddict(color%3D%22maroon%22))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20tickfont%3Ddict(color%3D%22maroon%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20showgrid%3DFalse%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20zeroline%3DTrue%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20yaxis2%3Ddict(%0A%20%20%20%20%20%20%20%20%20%20%20%20title%3Ddict(text%3D%22Unemployment%2C%20Inflation%22%2C%20font%3Ddict(color%3D%22blue%22))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20tickfont%3Ddict(color%3D%22blue%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20overlaying%3D%22y%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20side%3D%22right%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20showgrid%3DTrue%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20zeroline%3DFalse%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20range%3D%5B-2%2C%2015%5D%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20)%0A%20%20%20%20_fig2%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20---%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Inserting%20new%20columns%20%26%20deleting%20existing%20columns%0A%20%20%20%20In%20pandas%20we%20use%20%60insert%60%20to%20add%20columns%20at%20a%20specific%20position%2C%20and%20%60drop%60%20to%20remove%20columns.%0A%0A%20%20%20%20-%20**Inserting%20a%20new%20column**%20(e.g.%20log%20of%20GDP%20at%20position%202)%3A%0A%20%20%20%20%20%20%60%60%60python%0A%20%20%20%20%20%20%20%20%20%20%20%20USdata.insert(2%2C%20%22lnGDP%22%2C%20np.log(USdata%5B%22GDP%22%5D))%0A%20%20%20%20%20%20%60%60%60%0A%20%20%20%20-%20**Deleting%20an%20existing%20column**%3A%0A%20%20%20%20%20%20%60%60%60python%0A%20%20%20%20%20%20%20%20%20%20%20%20USdata%20%3D%20USdata.drop(columns%3D%5B%22FFR%22%5D)%0A%20%20%20%20%20%20%60%60%60%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(USdata%2C%20np)%3A%0A%20%20%20%20USdata.insert(2%2C%20%22lnGDP%22%2C%20np.log(USdata%5B%22GDP%22%5D))%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(USdata%2C%20preview)%3A%0A%20%20%20%20USdata%0A%20%20%20%20preview(USdata)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(USdata)%3A%0A%20%20%20%20USdata.drop(columns%3D%5B%22FFR%22%5D%2C%20inplace%3DTrue)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20---%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20%23%23%23%20Creating%20an%20entirely%20new%20dataframe%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(USdata)%3A%0A%20%20%20%20USdata2%20%3D%20USdata%5B%5B%22CPI%22%2C%20%22UR%22%2C%20%22GDP%22%2C%20%22lnGDP%22%5D%5D.copy()%0A%20%20%20%20USdata2%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%F0%9F%91%89%20**Exercise%201**%0A%0A%20%20%20%20Insert%20a%20new%20column%20into%20the%20dataframe%20by%20computing%20the%20logarithm%20of%20M2%2C%20suppress%20the%20display%20of%20its%20output%20and%20check%20that%20the%20operation%20was%20correctly%20implemented.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20---%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20%23%23%203.%20The%20Hodrick-Prescott%20filter%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20This%20filter%20intends%20to%20separate%20a%20time-series%20%24y_t%24%20into%20a%20trend%20%24%5Ctau_t%24%20and%20a%20cyclical%20component%20%24%5Cvarphi_t%24%20such%20that%3A%0A%0A%20%20%20%20%24%24y_t%20%3D%20%5Ctau_t%20%2B%20%5Cvarphi_t%20%5Ctag%7B1%7D%24%24%0A%0A%20%20%20%20-%20The%20trend%20is%20the%20long-run%20component%20of%20the%20time%20series%0A%20%20%20%20-%20The%20cyclical%20component%20is%20the%20short-run%20component%20of%20the%20time%20series%0A%20%20%20%20-%20Therefore%2C%20from%20(1)%20we%20get%3A%0A%0A%20%20%20%20%24%24%5Cvarphi_t%20%3D%20y_t%20-%20%5Ctau_t%20%5Ctag%7B2%7D%24%24%0A%0A%20%20%20%20The%20trick%20is%20to%20minimize%20eq.%20(2)%20subject%20to%20some%20given%20constraint%20on%20%24%5Cvarphi_t%24.%20The%20HP%20trend%20is%20given%20by%20the%20following%20minimization%20problem%20with%20respect%20to%20%24%5Ctau_t%24%3A%0A%0A%20%20%20%20%24%24%5Cmin%20_%7B%5Ctau_%7Bt%7D%7D%20%5Cleft%5C%7B%20%7B%5Ccal%7BL(%5Ctau)%7D%7D%20%3D%5Csum_%7Bt%3D1%7D%5E%7Bn%7D%20%5Cunderbrace%7B%5Cleft(y_%7Bt%7D-%5Ctau_%7Bt%7D%5Cright)%5E%7B2%7D%7D_%7B%3D%5Cvarphi_t%5E2%7D%20%2B%5C%20%7B%5Ccolor%7Bred%7D%5Clambda%7D%20%5Csum_%7Bt%3D2%7D%5E%7Bn-1%7D%20%5Cunderbrace%7B%5Cleft%5B%5Cleft(%5Ctau_%7Bt%2B1%7D-%5Ctau_%7Bt%7D%5Cright)-%5Cleft(%5Ctau_%7Bt%7D-%5Ctau_%7Bt-1%7D%5Cright)%5Cright%5D%5E%7B2%7D%7D_%7B%5Ctext%7Bconstraint%7D%7D%5Cright%5C%7D%20%5Ctag%7B3%7D%24%24%0A%0A%20%20%20%20where%20%24%5Clambda%24%20is%20the%20parameter%20that%20we%20set%20to%20obtain%20the%20%3Cspan%20style%3D'color%3Ared'%3Edesired%3C%2Fspan%3E%20smoothness%20in%20the%20trend.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20---%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20%23%23%23%20Setting%20the%20value%20of%20%24%5Clambda%24%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(%CE%BB_slider)%3A%0A%20%20%20%20%CE%BB%20%3D%20%CE%BB_slider.value%0A%20%20%20%20return%20(%CE%BB%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20%CE%BB_slider%20%3D%20mo.ui.slider(%0A%20%20%20%20%20%20%20%20start%3D0%2C%0A%20%20%20%20%20%20%20%20stop%3D150000%2C%0A%20%20%20%20%20%20%20%20step%3D800%2C%0A%20%20%20%20%20%20%20%20value%3D1600%2C%0A%20%20%20%20%20%20%20%20label%3D%22%CE%BB%22%2C%0A%20%20%20%20%20%20%20%20show_value%3DTrue%2C%0A%20%20%20%20%20%20%20%20full_width%3DTrue%2C%0A%20%20%20%20)%0A%20%20%20%20%CE%BB_slider.style(%7B%22width%22%3A%20%22500px%22%7D)%0A%20%20%20%20return%20(%CE%BB_slider%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20---%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20HP%20trend%20on%20a%20single%20variable%2Fcolumn%20name%0A%0A%20%20%20%20To%20compute%20the%20HP%20trend%20on%20a%20single%20column%20(%60lnGDP%60)%3A%0A%0A%20%20%20%20%60%60%60python%0A%20%20%20%20%20%20%20%20%20%20%20%20GDP_trend%20%20%3D%20hp_trend(USdata%5B%22lnGDP%22%5D.values%2C%20%CE%BB)%0A%20%20%20%20%20%20%20%20%20%20%20%20GDP_cycles%20%3D%20USdata%5B%22lnGDP%22%5D.values%20-%20GDP_trend%0A%20%20%20%20%60%60%60%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(USdata%2C%20hp_trend%2C%20%CE%BB)%3A%0A%20%20%20%20GDP_trend%20%3D%20hp_trend(USdata.lnGDP.values%2C%20%CE%BB)%0A%20%20%20%20GDP_cycles%20%3D%20USdata.lnGDP.values%20-%20GDP_trend%0A%20%20%20%20return%20GDP_cycles%2C%20GDP_trend%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(GDP_trend%2C%20USdata%2C%20go)%3A%0A%20%20%20%20_fig3%20%3D%20go.Figure()%0A%20%20%20%20_fig3.add_trace(%0A%20%20%20%20%20%20%20%20go.Scatter(x%3DUSdata.Quarters%2C%20y%3DUSdata.lnGDP%2C%20mode%3D%22lines%22%2C%20name%3D%22GDP%22)%0A%20%20%20%20)%0A%20%20%20%20_fig3.add_trace(%0A%20%20%20%20%20%20%20%20go.Scatter(x%3DUSdata.Quarters%2C%20y%3DGDP_trend%2C%20mode%3D%22lines%22%2C%20name%3D%22HP_trend%22)%0A%20%20%20%20)%0A%20%20%20%20_fig3.update_layout(%0A%20%20%20%20%20%20%20%20height%3D450%2C%0A%20%20%20%20%20%20%20%20hovermode%3D%22x%22%2C%0A%20%20%20%20%20%20%20%20title_text%3D%22GDP%20versus%20GDP%20trend%20(US%3A%201960Q1%E2%80%932023Q4)%22%2C%0A%20%20%20%20%20%20%20%20title_x%3D0.5%2C%0A%20%20%20%20%20%20%20%20xaxis%3Ddict(title%3D%22Quarters%22%2C%20range%3D%5B%221959-04-01%22%2C%20%222024-10-01%22%5D)%2C%0A%20%20%20%20%20%20%20%20yaxis_title%3D%22Billion%20dollars%20(logarithmic)%22%2C%0A%20%20%20%20%20%20%20%20margin%3Ddict(l%3D70%2C%20r%3D60%2C%20t%3D70%2C%20b%3D60)%2C%0A%20%20%20%20)%0A%20%20%20%20_fig3%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(GDP_cycles%2C%20USdata%2C%20go)%3A%0A%20%20%20%20_fig4%20%3D%20go.Figure()%0A%20%20%20%20_fig4.add_trace(%0A%20%20%20%20%20%20%20%20go.Bar(x%3DUSdata.Quarters%2C%20y%3DGDP_cycles%2C%20name%3D%22HP_GDP%22%2C%20marker_color%3D%22Navy%22)%0A%20%20%20%20)%0A%20%20%20%20_fig4.update_layout(%0A%20%20%20%20%20%20%20%20height%3D450%2C%0A%20%20%20%20%20%20%20%20hovermode%3D%22x%22%2C%0A%20%20%20%20%20%20%20%20bargap%3D0.4%2C%0A%20%20%20%20%20%20%20%20title_text%3D%22The%20Output-gap%3A%20HP%20trended%20business%20cycles%20(US%3A%201960Q1%E2%80%932023Q4)%22%2C%0A%20%20%20%20%20%20%20%20title_x%3D0.5%2C%0A%20%20%20%20%20%20%20%20xaxis%3Ddict(title%3D%22Quarters%22%2C%20range%3D%5B%221959-04-01%22%2C%20%222024-10-01%22%5D)%2C%0A%20%20%20%20%20%20%20%20yaxis_title%3D%22Percentage%20deviations%22%2C%0A%20%20%20%20%20%20%20%20margin%3Ddict(l%3D70%2C%20r%3D60%2C%20t%3D70%2C%20b%3D60)%2C%0A%20%20%20%20)%0A%20%20%20%20_fig4%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20---%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20%23%23%23%20The%20HP%20trend%20on%20an%20entire%20data%20set%0A%0A%20%20%20%20There%20are%20two%20significant%20shortcomings%20in%20the%20steps%20we%20undertook%20above%3A%0A%0A%20%20%20%20-%20Firstly%2C%20it%20is%20terribly%20inefficient%3A%20if%20we%20have%20a%20dataframe%20with%2C%20e.g.%2C%2020%20variables%2C%20we%20must%20repeat%20the%20same%20procedure%2020%20times.%0A%0A%20%20%20%20-%20Secondly%2C%20as%20the%20HP-trend%20can%20not%20trend%20non-numerical%20data%20(like%20dates%20in%20the%20first%20column%20of%20the%20original%20data%20frame%3A%20%22Quarters%22)%2C%20this%20creates%20further%20problems%20if%20we%20want%20to%20work%20with%20dataframes%20(which%20we%20should).%0A%0A%20%20%20%20By%20using%20the%20%3Ckbd%3Ehp_trend_df%3C%2Fkbd%3E%20function%20instead%20of%20%3Ckbd%3Ehp_trend%3C%2Fkbd%3E%2C%20the%20two%20problems%20above%20will%20vanish.%20The%20function%20%3Ckbd%3Ehp_trend_df%3C%2Fkbd%3E%20uses%20the%20main%20function%20%3Ckbd%3Ehp_trend%3C%2Fkbd%3E%20and%20has%20three%20useful%20properties%3A%0A%0A%20%20%20%20-%20It%20allows%20the%20trend%20to%20be%20applied%20to%20an%20entire%20dataframe%20and%20produces%20a%20new%20dataframe%20as%20the%20output.%0A%0A%20%20%20%20-%20It%20keeps%20the%20first%20non-numeric%20column%20(date%20object%3A%20year%2C%20quarter%2C%20month%2C%20etc.)%20of%20the%20original%20data%20frame%20as%20the%20first%20column%20of%20the%20new%20data%20frame.%0A%0A%20%20%20%20-%20This%20is%20extremely%20convenient%20because%20we%20will%20have%20all%20the%20functionalities%20of%20a%20data%20frame%20when%20producing%20further%20operations%20on%20the%20new%20trended%20results.%0A%0A%20%20%20%20To%20compute%20the%20HP%20trend%20associated%20with%20each%20variable%20(column)%20of%20the%20entire%20data%20set%2C%20all%20we%20need%20to%20do%20is%20to%20apply%20the%20following%20code%3A%0A%0A%20%20%20%20%60%60%60python%0A%20%20%20%20%20%20%20%20USdata_trends%20%3D%20hp_trend_df(USdata%2C%20%CE%BB)%0A%20%20%20%20%60%60%60%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20---%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20%23%23%23%20Computing%20the%20trend%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(USdata%2C%20hp_trend_df%2C%20preview%2C%20%CE%BB)%3A%0A%20%20%20%20USdata_trends%20%3D%20hp_trend_df(USdata%2C%20%CE%BB)%0A%20%20%20%20preview(USdata_trends)%0A%20%20%20%20return%20(USdata_trends%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20---%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Computing%20the%20cycles%0A%0A%20%20%20%20According%20to%20eq.%20(2)%2C%20the%20cycles%20are%20computed%20as%20the%20difference%20between%20the%20original%20time%20series%20%24(y_t)%24%20and%20its%20long%20term%20trend%20%24(%5Ctau_t)%24%3A%0A%0A%20%20%20%20%24%24%5Cvarphi_t%20%3D%20y_t%20-%20%5Ctau_t%20%5Ctag%7B2a%7D%24%24%0A%0A%20%20%20%20As%20%24y_t%24%20and%20%24%5Ctau_t%24%20are%20two%20dataframes%20(respectively%2C%3Ckbd%3EUSdata%3C%2Fkbd%3E%20and%20%3Ckbd%3EUSdata_trends%3C%2Fkbd%3E)%2C%20the%20computation%20of%20the%20difference%20between%20the%20two%20dataframes%20(column%20by%20column)%20can%20be%20easily%20accomplished%20by%20using%20some%20simple%20binary%20operations%20involving%20the%20two%20data%20frames.%20The%20function%20%3Ckbd%3Edf_arith_operations%3C%2Fkbd%3E)%20can%20do%20that%20very%20easily%2C%20just%20by%20typing%3A%0A%0A%20%20%20%20%60%60%60python%0A%20%20%20%20%20%20%20%20USdata_cycles%20%3D%20df_arith_operations(USdata%2C%20USdata_trends%2C%20np.subtract)%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20This%20function%20will%20pass%20the%20binary%20operation%20%3Ckbd%3Enp.subtract%3C%2Fkbd%3E%20as%20a%20function%20and%20it%20will%20apply%20it%20column-wise%20accross%20the%20two%20data%20frames%2C%20**skipping%20the%20first%20column%20(the%20date%20object)**.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(USdata%2C%20USdata_trends%2C%20df_arith_operations%2C%20np%2C%20preview)%3A%0A%20%20%20%20USdata_cycles%20%3D%20df_arith_operations(USdata%2C%20USdata_trends%2C%20np.subtract)%0A%20%20%20%20preview(USdata_cycles)%0A%20%20%20%20return%20(USdata_cycles%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20---%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20%23%23%23%20Some%20plotting%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(USdata%2C%20USdata_cycles%2C%20go)%3A%0A%20%20%20%20_fig5%20%3D%20go.Figure()%0A%20%20%20%20_fig5.add_trace(%0A%20%20%20%20%20%20%20%20go.Bar(x%3DUSdata.Quarters%2C%20y%3DUSdata_cycles.lnGDP%2C%20marker_color%3D%22Navy%22)%0A%20%20%20%20)%0A%20%20%20%20_fig5.update_layout(%0A%20%20%20%20%20%20%20%20height%3D450%2C%0A%20%20%20%20%20%20%20%20hovermode%3D%22x%22%2C%0A%20%20%20%20%20%20%20%20bargap%3D0.4%2C%0A%20%20%20%20%20%20%20%20title_text%3D%22The%20Output-gap%20using%20the%20HP%20trend%20(US%3A%201960Q1%E2%80%932023Q4)%22%2C%0A%20%20%20%20%20%20%20%20title_x%3D0.5%2C%0A%20%20%20%20%20%20%20%20xaxis%3Ddict(title%3D%22Quarters%22%2C%20range%3D%5B%221959-04-01%22%2C%20%222024-10-01%22%5D)%2C%0A%20%20%20%20%20%20%20%20yaxis_title%3D%22Percentage%20deviations%22%2C%0A%20%20%20%20%20%20%20%20margin%3Ddict(l%3D70%2C%20r%3D60%2C%20t%3D70%2C%20b%3D60)%2C%0A%20%20%20%20)%0A%20%20%20%20_fig5%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(USdata%2C%20USdata_cycles%2C%20go)%3A%0A%20%20%20%20_fig6%20%3D%20go.Figure()%0A%20%20%20%20_fig6.add_trace(%0A%20%20%20%20%20%20%20%20go.Bar(x%3DUSdata.Quarters%2C%20y%3DUSdata_cycles.GDP%2C%20marker_color%3D%22maroon%22)%0A%20%20%20%20)%0A%20%20%20%20_fig6.update_layout(%0A%20%20%20%20%20%20%20%20height%3D450%2C%0A%20%20%20%20%20%20%20%20hovermode%3D%22x%22%2C%0A%20%20%20%20%20%20%20%20bargap%3D0.4%2C%0A%20%20%20%20%20%20%20%20title_text%3D%22Incorrect%20measure%20of%20the%20Output-gap%20in%20the%20US%3A%201960Q1%E2%80%932023Q4%22%2C%0A%20%20%20%20%20%20%20%20title_x%3D0.5%2C%0A%20%20%20%20%20%20%20%20xaxis%3Ddict(title%3D%22Quarters%22%2C%20range%3D%5B%221959-04-01%22%2C%20%222024-10-01%22%5D)%2C%0A%20%20%20%20%20%20%20%20yaxis_title%3D%22Percentage%20deviations%22%2C%0A%20%20%20%20%20%20%20%20margin%3Ddict(l%3D70%2C%20r%3D60%2C%20t%3D70%2C%20b%3D60)%2C%0A%20%20%20%20)%0A%20%20%20%20_fig6%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(USdata%2C%20USdata_cycles%2C%20go)%3A%0A%20%20%20%20_fig7%20%3D%20go.Figure()%0A%20%20%20%20_fig7.add_trace(%0A%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3DUSdata.Quarters%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3DUSdata_cycles.CPI%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22markers%2Blines%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(symbol%3D%22circle%22%2C%20size%3D5.5%2C%20color%3D%22darkblue%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(width%3D0.3)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22CPI%22%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20)%0A%20%20%20%20_fig7.update_layout(%0A%20%20%20%20%20%20%20%20height%3D450%2C%0A%20%20%20%20%20%20%20%20title_text%3D%22The%20inflation-gap%20in%20the%20US%3A%201960Q1%E2%80%932023Q4%22%2C%0A%20%20%20%20%20%20%20%20title_x%3D0.5%2C%0A%20%20%20%20%20%20%20%20hovermode%3D%22x%22%2C%0A%20%20%20%20%20%20%20%20margin%3Ddict(l%3D70%2C%20r%3D60%2C%20t%3D70%2C%20b%3D60)%2C%0A%20%20%20%20%20%20%20%20xaxis%3Ddict(%0A%20%20%20%20%20%20%20%20%20%20%20%20title%3D%22Quarterly%20observations%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20tickformat%3D%22%25Y%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20range%3D%5B%221959-04-01%22%2C%20%222024-10-01%22%5D%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20yaxis%3Ddict(title%3D%22Percentage%20deviations%22%2C%20range%3D%5B-6%2C%207%5D)%2C%0A%20%20%20%20)%0A%20%20%20%20_fig7%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(USdata%2C%20USdata_trends%2C%20go)%3A%0A%20%20%20%20_fig8%20%3D%20go.Figure()%0A%20%20%20%20_fig8.add_trace(%0A%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3DUSdata.Quarters%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3DUSdata_trends.UR%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22lines%2Bmarkers%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(symbol%3D%22circle%22%2C%20size%3D4.5%2C%20color%3D%22darkred%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(width%3D0.3)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22NUR%20(u%E2%81%BF)%22%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20)%0A%20%20%20%20_fig8.add_trace(%0A%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3DUSdata%5B%22Quarters%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3DUSdata%5B%22UR%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22lines%2Bmarkers%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(symbol%3D%22circle%22%2C%20size%3D5%2C%20color%3D%22blue%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(width%3D0.3)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Unemployment%20rate%20(u)%22%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20)%0A%20%20%20%20_fig8.update_layout(%0A%20%20%20%20%20%20%20%20margin%3Ddict(l%3D70%2C%20r%3D60%2C%20t%3D70%2C%20b%3D60)%2C%0A%20%20%20%20%20%20%20%20height%3D450%2C%0A%20%20%20%20%20%20%20%20font_family%3D%22Calibri%22%2C%0A%20%20%20%20%20%20%20%20font_size%3D15%2C%0A%20%20%20%20%20%20%20%20title_text%3D(%0A%20%20%20%20%20%20%20%20%20%20%20%20%22The%20Unemployment%20Rate%20vs%20the%20Natural%20Unemployment%20Rate%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%22(trend)%20in%20the%20US%3A%201960Q1%E2%80%932023Q4%22%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20title_x%3D0.5%2C%0A%20%20%20%20%20%20%20%20hovermode%3D%22x%22%2C%0A%20%20%20%20%20%20%20%20legend%3Ddict(x%3D0.02%2C%20y%3D0.94)%2C%0A%20%20%20%20%20%20%20%20xaxis%3Ddict(%0A%20%20%20%20%20%20%20%20%20%20%20%20title%3D%22Quarterly%20observations%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20tickformat%3D%22%25Y%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20range%3D%5B%221959-04-01%22%2C%20%222024-10-01%22%5D%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20yaxis_title%3D%22Percentage%20points%22%2C%0A%20%20%20%20)%0A%20%20%20%20_fig8%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%F0%9F%91%89%20**Exercise%202**%0A%0A%20%20%20%20Produce%20a%20plot%20involving%20the%20**logarithmic**%20value%20of%20the%20Unemployment%20Rate%20and%20its%20long-term%20trend.%20Copy%20the%20code%20from%20the%20previous%20plot%20and%20introduce%20the%20required%20changes%3A%20variable%20names%2C%20title%2C%20and%20variable%20labels.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Exercise%202%20%E2%80%94%20your%20code%20here%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%F0%9F%91%89%20**Exercise%203**%0A%0A%20%20%20%20In%20the%20slides%20we%20saw%20that%20if%20a%20macroeconomic%20variable%20has%20a%20clear%20trend%2C%20the%20HP%20trend%20should%20be%20applied%20to%20the%20**logarithmic**%20value%20of%20the%20original%20observations.%20Using%20the%20case%20of%20real%20GDP%2C%20confirm%20that%20dividing%20its%20HP%20cycles%20(obtained%20*without*%20logarithms)%20by%20its%20corresponding%20trend%20(also%20without%20logarithms)%20yields%20the%20same%20%22correct%22%20measure%20%20%20%20of%20business%20cycles%20as%20applying%20logarithms%20to%20the%20original%20observations%20(numerically%20or%20graphically).%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Exercise%203%20%E2%80%94%20your%20code%20here%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20---%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20%23%23%204.%20Impulse%20Response%20Functions%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20%23%23%23%20AR(1)%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Consider%20the%20following%20AR(1)%20process%3A%0A%0A%20%20%20%20%24%24y_%7Bt%2B1%7D%20%3D%20%5Ccolor%7Bblueviolet%7D%7Ba%7D%20%5Ccdot%20y_t%20%2B%20%5Cvarepsilon_%7Bt%2B1%7D%20%5Ctag%7B1%7D%24%24%0A%0A%20%20%20%20Assume%20%24y_1%20%3D%200%24%2C%20%24%5Cvarepsilon_2%20%3D%201%24%2C%20and%20%24%5Cvarepsilon_t%20%3D%200%24%20for%20all%20%24t%20%5Cneq%202%24.%0A%0A%20%20%20%20This%20implies%20%24y_2%20%3D%201%24.%20What%20happens%20next%2C%20if%20there%20are%20no%20more%20shocks%3F%0A%20%20%20%20The%20IRF%20of%20%24y%24%20provides%20the%20answer.%20The%20dynamics%20depend%20crucially%20on%20%24%5Ccolor%7Bblueviolet%7D%7Ba%7D%24%3A%0A%0A%20%20%20%20%24%24%5Ccolor%7Bblueviolet%7D%7Ba%7D%20%3D%20%5C%7B0%2C%5C%2C%200.5%2C%5C%2C%200.9%2C%5C%2C%200.99%2C%5C%2C%201%2C%5C%2C%201.01%5C%7D%24%24%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(go%2C%20np)%3A%0A%20%20%20%20%23%20AR(1)%20%E2%80%94%20single%20case%20a%20%3D%200.5%0A%20%20%20%20_n%20%3D%2020%0A%20%20%20%20_y1%20%3D%200.0%0A%20%20%20%20_%CE%B5%20%3D%20np.zeros(_n)%0A%20%20%20%20_%CE%B5%5B1%5D%20%3D%201.0%0A%20%20%20%20_a%20%3D%200.5%0A%20%20%20%20_y%20%3D%20np.zeros(_n)%0A%20%20%20%20_y%5B0%5D%20%3D%20_y1%0A%20%20%20%20for%20_t%20in%20range(_n%20-%201)%3A%0A%20%20%20%20%20%20%20%20_y%5B_t%20%2B%201%5D%20%3D%20_a%20*%20_y%5B_t%5D%20%2B%20_%CE%B5%5B_t%20%2B%201%5D%0A%0A%20%20%20%20_fig9%20%3D%20go.Figure()%0A%20%20%20%20_fig9.add_trace(%0A%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D_y%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22markers%2Blines%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(symbol%3D%22circle%22%2C%20size%3D6%2C%20color%3D%22blue%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(width%3D0.3)%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20)%0A%20%20%20%20_fig9.update_layout(%0A%20%20%20%20%20%20%20%20margin%3Ddict(l%3D70%2C%20r%3D60%2C%20t%3D70%2C%20b%3D60)%2C%0A%20%20%20%20%20%20%20%20title_text%3D%22A%20stochastic%20AR(1)%20process%22%2C%0A%20%20%20%20%20%20%20%20title_x%3D0.5%2C%0A%20%20%20%20%20%20%20%20hovermode%3D%22x%22%2C%0A%20%20%20%20%20%20%20%20height%3D450%2C%0A%20%20%20%20%20%20%20%20%23%20Exercise%203%20%E2%80%94%20your%20code%20here%0A%20%20%20%20)%0A%20%20%20%20_fig9%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(go%2C%20np)%3A%0A%20%20%20%20%23%20AR(1)%20%E2%80%94%20six%20values%20of%20a%0A%20%20%20%20_%F0%9D%95%9F%20%3D%2030%0A%20%20%20%20_%F0%9D%95%92%20%3D%20(0.0%2C%200.5%2C%200.9%2C%200.99%2C%201.0%2C%201.01)%0A%20%20%20%20%F0%9D%95%96%20%3D%20np.zeros((6%2C%20_%F0%9D%95%9F))%0A%20%20%20%20%F0%9D%95%96%5B%3A%2C%201%5D%20%3D%201.0%0A%20%20%20%20_%F0%9D%95%AA%20%3D%20np.zeros((6%2C%20_%F0%9D%95%9F))%0A%0A%20%20%20%20for%20%F0%9D%95%9C%20in%20range(6)%3A%0A%20%20%20%20%20%20%20%20for%20%F0%9D%95%A5%20in%20range(_%F0%9D%95%9F%20-%201)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20_%F0%9D%95%AA%5B%F0%9D%95%9C%2C%20%F0%9D%95%A5%20%2B%201%5D%20%3D%20_%F0%9D%95%92%5B%F0%9D%95%9C%5D%20*%20_%F0%9D%95%AA%5B%F0%9D%95%9C%2C%20%F0%9D%95%A5%5D%20%2B%20%F0%9D%95%96%5B%F0%9D%95%9C%2C%20%F0%9D%95%A5%20%2B%201%5D%0A%0A%20%20%20%20_colors%20%3D%20%5B%22blue%22%2C%20%22red%22%2C%20%22grey%22%2C%20%22purple%22%2C%20%22darkblue%22%2C%20%22orange%22%5D%0A%20%20%20%20_names%20%3D%20%5B%22%F0%9D%92%82%20%3D%200%22%2C%20%22%F0%9D%92%82%20%3D%200.5%22%2C%20%22%F0%9D%92%82%20%3D%200.9%22%2C%20%22%F0%9D%92%82%20%3D%200.99%22%2C%20%22%F0%9D%92%82%20%3D%201.0%22%2C%20%22%F0%9D%92%82%20%3D%201.01%22%5D%0A%0A%20%20%20%20_fig10%20%3D%20go.Figure()%0A%20%20%20%20for%20_k%20in%20range(6)%3A%0A%20%20%20%20%20%20%20%20_fig10.add_trace(%0A%20%20%20%20%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20y%3D_%F0%9D%95%AA%5B_k%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22markers%2Blines%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(size%3D6)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(width%3D0.5%2C%20color%3D_colors%5B_k%5D)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20name%3D_names%5B_k%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20_fig10.update_layout(%0A%20%20%20%20%20%20%20%20margin%3Ddict(l%3D70%2C%20r%3D60%2C%20t%3D70%2C%20b%3D60)%2C%0A%20%20%20%20%20%20%20%20title_text%3D%22Impulse%20Response%20Functions%20(IRF)%20from%20an%20AR(1)%20process%22%2C%0A%20%20%20%20%20%20%20%20title_x%3D0.5%2C%0A%20%20%20%20%20%20%20%20hovermode%3D%22x%22%2C%0A%20%20%20%20%20%20%20%20height%3D500%2C%0A%20%20%20%20)%0A%20%20%20%20_fig10%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20%23%23%23%20AR(2)%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Consider%20the%20AR(2)%20process%3A%0A%0A%20%20%20%20%24%24y_%7Bt%2B1%7D%20%3D%20%5Ccolor%7Bred%7D%7Ba%7D%5C%2C%20y_t%20%2B%20%5Ccolor%7Bblueviolet%7D%7Bb%7D%5C%2C%20y_%7Bt-1%7D%20%2B%20%5Cvarepsilon_%7Bt%2B1%7D%2C%0A%20%20%20%20%5Cquad%20%5Cvarepsilon_t%20%5Csim%20N(0%2C1)%20%5Ctag%7B2%7D%24%24%0A%0A%20%20%20%20Assume%20%24y_1%20%3D%20y_2%20%3D%200%24%2C%20%24%5Cvarepsilon_3%20%3D%201%24%2C%20%24%5Cvarepsilon_t%20%3D%200%24%20for%20all%20%24t%20%5Cneq%203%24%2C%0A%20%20%20%20so%20%24y_3%20%3D%201%24.%0A%0A%20%20%20%20Fix%20%24%5Ccolor%7Bred%7D%7Bb%20%3D%20-0.9%7D%24%20and%20let%20%24%5Ccolor%7Bblueviolet%7D%7Ba%7D%20%5Cin%20%5C%7B1.85%2C%5C%2C%201.895%2C%5C%2C%201.9%2C%5C%2C%201.9005%5C%7D%24.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(go%2C%20np)%3A%0A%20%20%20%20%23%20AR(2)%20%E2%80%94%20four%20values%20of%20a%2C%20b%20%3D%20-0.9%0A%20%20%20%20_%F0%9D%90%8D%20%3D%2099%0A%20%20%20%20_%F0%9D%90%9A%20%3D%20(1.85%2C%201.895%2C%201.9%2C%201.9005)%0A%20%20%20%20%F0%9D%90%9B%20%3D%20-0.9%0A%20%20%20%20_%F0%9D%9B%9C%20%3D%20np.zeros((4%2C%20_%F0%9D%90%8D%20%2B%201))%0A%20%20%20%20_%F0%9D%9B%9C%5B%3A%2C%202%5D%20%3D%201.0%0A%20%20%20%20_%F0%9D%90%B2%20%3D%20np.zeros((4%2C%20_%F0%9D%90%8D%20%2B%201))%0A%0A%20%20%20%20for%20_k%20in%20range(4)%3A%0A%20%20%20%20%20%20%20%20for%20_t%20in%20range(_%F0%9D%90%8D%20-%201)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20_%F0%9D%90%B2%5B_k%2C%20_t%20%2B%202%5D%20%3D%20_%F0%9D%90%9A%5B_k%5D%20*%20_%F0%9D%90%B2%5B_k%2C%20_t%20%2B%201%5D%20%2B%20%F0%9D%90%9B%20*%20_%F0%9D%90%B2%5B_k%2C%20_t%5D%20%2B%20_%F0%9D%9B%9C%5B_k%2C%20_t%20%2B%202%5D%0A%0A%20%20%20%20_colors2%20%3D%20%5B%22blue%22%2C%20%22navy%22%2C%20%22gray%22%2C%20%22darkorange%22%5D%0A%20%20%20%20_names2%20%3D%20%5B%22%F0%9D%92%82%20%3D%201.85%22%2C%20%22%F0%9D%92%82%20%3D%201.895%22%2C%20%22%F0%9D%92%82%20%3D%201.9%22%2C%20%22%F0%9D%92%82%20%3D%201.9005%22%5D%0A%0A%20%20%20%20_fig11%20%3D%20go.Figure()%0A%20%20%20%20for%20_k%20in%20range(4)%3A%0A%20%20%20%20%20%20%20%20_fig11.add_trace(%0A%20%20%20%20%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20y%3D_%F0%9D%90%B2%5B_k%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22markers%2Blines%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(size%3D5)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(width%3D0.5%2C%20color%3D_colors2%5B_k%5D)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20name%3D_names2%5B_k%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20_fig11.update_layout(%0A%20%20%20%20%20%20%20%20margin%3Ddict(l%3D70%2C%20r%3D60%2C%20t%3D70%2C%20b%3D60)%2C%0A%20%20%20%20%20%20%20%20title_text%3D%22Impulse%20Response%20Functions%20(IRF)%20from%20an%20AR(2)%20process%22%2C%0A%20%20%20%20%20%20%20%20title_x%3D0.5%2C%0A%20%20%20%20%20%20%20%20hovermode%3D%22x%22%2C%0A%20%20%20%20%20%20%20%20height%3D500%2C%0A%20%20%20%20%20%20%20%20xaxis_range%3D%5B-1%2C%20102%5D%2C%0A%20%20%20%20)%0A%20%20%20%20_fig11%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20---%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20%23%23%23%20VAR(3)%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20A%20similar%20reasoning%20applies%20to%20the%20general%20model%3A%0A%0A%20%20%20%20%24%24X_%7Bt%2B1%7D%20%3D%20A%20%2B%20B%20X_t%20%2B%20C%20%5Cvarepsilon_%7Bt%2B1%7D%20%5Ctag%7B3%7D%24%24%0A%0A%20%20%20%20where%20%24B%2C%20C%24%20are%20%24n%20%5Ctimes%20n%24%20matrices%20and%20%24X_%7Bt%2B1%7D%2C%20X_t%2C%20A%2C%20%5Cvarepsilon_%7Bt%2B1%7D%24%20are%20%24n%20%5Ctimes%201%24%20vectors.%0A%0A%20%20%20%20Consider%20the%20following%20VAR(3)%3A%0A%0A%20%20%20%20%24%24X_%7Bt%2B1%7D%20%3D%20%5Cbegin%7Bbmatrix%7D%20z_%7Bt%2B1%7D%20%5C%5C%20w_%7Bt%2B1%7D%20%5C%5C%20v_%7Bt%2B1%7D%20%5Cend%7Bbmatrix%7D%20%5Ctag%7B4%7D%24%24%0A%0A%20%20%20%20with%20matrices%3A%0A%0A%20%20%20%20%24%24A%20%3D%20%5Cbegin%7Bbmatrix%7D%200%20%5C%5C%200%20%5C%5C%200%20%5Cend%7Bbmatrix%7D%2C%20%5Cquad%0A%20%20%20%20B%20%3D%20%5Cbegin%7Bbmatrix%7D%200.97%20%26%200.10%20%26%20-0.05%20%5C%5C%20-0.3%20%26%200.8%20%26%200.05%20%5C%5C%200.01%20%26%20-0.04%20%26%200.96%20%5Cend%7Bbmatrix%7D%2C%20%5Cquad%0A%20%20%20%20C%20%3D%20%5Cbegin%7Bbmatrix%7D%20%5Ccolor%7Bblueviolet%7D%7B1.0%7D%20%26%200%20%26%200%20%5C%5C%200%20%26%200%20%26%200%20%5C%5C%200%20%26%200%20%26%200%20%5Cend%7Bbmatrix%7D%20%5Ctag%7B5%7D%24%24%0A%0A%20%20%20%20Initial%20conditions%3A%20%24X_1%20%3D%20%5B0%2C%200%2C%200%5D%24.%20The%20shock%20hits%20only%20%24z_t%24%20(blue%20entry%20in%20%24C%24)%20at%20%24t%20%3D%203%24.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np)%3A%0A%20%20%20%20%F0%9D%90%B4%20%3D%20np.zeros((3%2C%201))%0A%20%20%20%20%F0%9D%90%B5%20%3D%20np.array(%5B%5B0.97%2C%200.10%2C%20-0.05%5D%2C%20%5B-0.30%2C%200.80%2C%200.05%5D%2C%20%5B0.01%2C%20-0.04%2C%200.96%5D%5D)%0A%20%20%20%20%F0%9D%90%B6%20%3D%20np.array(%5B%5B1.0%2C%200.0%2C%200.0%5D%2C%20%5B0.0%2C%200.0%2C%200.0%5D%2C%20%5B0.0%2C%200.0%2C%200.0%5D%5D)%0A%20%20%20%20return%20A%2C%20B%2C%20C%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20%F0%9D%91%A71_slider%20%3D%20mo.ui.slider(%0A%20%20%20%20%20%20%20%20start%3D0.0%2C%0A%20%20%20%20%20%20%20%20stop%3D5.0%2C%0A%20%20%20%20%20%20%20%20step%3D1.0%2C%0A%20%20%20%20%20%20%20%20value%3D1.0%2C%0A%20%20%20%20%20%20%20%20label%3D%22%F0%9D%91%A7%E2%82%81%22%2C%0A%20%20%20%20%20%20%20%20show_value%3DTrue%2C%0A%20%20%20%20)%0A%20%20%20%20%F0%9D%91%A71_slider%0A%20%20%20%20return%20(z1_slider%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(A%2C%20B%2C%20C%2C%20go%2C%20np%2C%20z1_slider)%3A%0A%20%20%20%20%F0%9D%91%81%20%3D%2040%0A%20%20%20%20%F0%9D%91%8B1%20%3D%20np.zeros((3%2C%201))%0A%20%20%20%20_%F0%9D%9C%80%20%3D%20np.zeros((3%2C%20%F0%9D%91%81))%0A%20%20%20%20_%F0%9D%9C%80%5B0%2C%202%5D%20%3D%20float(%F0%9D%91%A71_slider.value)%0A%20%20%20%20%F0%9D%91%8B%20%3D%20np.zeros((3%2C%20%F0%9D%91%81))%0A%20%20%20%20%F0%9D%91%8B%5B%3A%2C%200%3A1%5D%20%3D%20%F0%9D%91%8B1%0A%0A%20%20%20%20for%20_t%20in%20range(%F0%9D%91%81%20-%201)%3A%0A%20%20%20%20%20%20%20%20%F0%9D%91%8B%5B%3A%2C%20_t%20%2B%201%20%3A%20_t%20%2B%202%5D%20%3D%20%F0%9D%90%B4%20%2B%20%F0%9D%90%B5%20%40%20%F0%9D%91%8B%5B%3A%2C%20_t%20%3A%20_t%20%2B%201%5D%20%2B%20%F0%9D%90%B6%20%40%20_%F0%9D%9C%80%5B%3A%2C%20_t%20%2B%201%20%3A%20_t%20%2B%202%5D%0A%0A%20%20%20%20_names3%20%3D%20%5B%22%F0%9D%92%9B(%F0%9D%92%95)%22%2C%20%22%F0%9D%92%98(%F0%9D%92%95)%22%2C%20%22%F0%9D%92%97(%F0%9D%92%95)%22%5D%0A%20%20%20%20_colors3%20%3D%20%5B%22blue%22%2C%20%22gray%22%2C%20%22navy%22%5D%0A%0A%20%20%20%20_fig12%20%3D%20go.Figure()%0A%20%20%20%20for%20_i%20in%20range(3)%3A%0A%20%20%20%20%20%20%20%20_fig12.add_trace(%0A%20%20%20%20%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20y%3D%F0%9D%91%8B%5B_i%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22markers%2Blines%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(symbol%3D%22circle%22%2C%20size%3D6)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(width%3D0.3%2C%20color%3D_colors3%5B_i%5D)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20name%3D_names3%5B_i%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20_fig12.update_layout(%0A%20%20%20%20%20%20%20%20margin%3Ddict(l%3D70%2C%20r%3D60%2C%20t%3D70%2C%20b%3D60)%2C%0A%20%20%20%20%20%20%20%20title_text%3D%22Impulse%20Response%20Functions%20(IRF)%20from%20a%20VAR(3)%20process%22%2C%0A%20%20%20%20%20%20%20%20title_x%3D0.5%2C%0A%20%20%20%20%20%20%20%20height%3D500%2C%0A%20%20%20%20%20%20%20%20hovermode%3D%22x%22%2C%0A%20%20%20%20%20%20%20%20legend%3Ddict(x%3D0.9%2C%20y%3D0.95)%2C%0A%20%20%20%20%20%20%20%20xaxis_range%3D%5B-0.5%2C%2041%5D%2C%0A%20%20%20%20)%0A%20%20%20%20_fig12%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20---%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20%23%23%205.%20How%20to%20produce%20a%20nice%20plot%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(USdata%2C%20go)%3A%0A%20%20%20%20%23%20to%20produce%20a%20plot%20named%20pic%2C%20with%20Quarters%20in%20the%20x-axis%2C%20and%20CPI%20and%20UR%20in%20the%20y-axis%2C%20using%20the%20USdata%20dataframe%20we%20should%20start%20by%20doing%3A%0A%0A%20%20%20%20pic%20%3D%20go.Figure()%0A%20%20%20%20pic.add_trace(%0A%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3DUSdata.Quarters%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3DUSdata.CPI%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22markers%2Blines%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22CPI%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(symbol%3D%22circle%22%2C%20size%3D5%2C%20color%3D%22blue%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(width%3D0.5)%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20)%0A%20%20%20%20%23%20Then%20we%20can%20add%20new%20features%20to%20our%20plot%20by%20using%20a%20function%20in%20Plotly%3A%20add_trace%0A%20%20%20%20pic.add_trace(%0A%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3DUSdata.Quarters%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3DUSdata.UR%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22markers%2Blines%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22UR%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(symbol%3D%22circle%22%2C%20size%3D5%2C%20color%3D%22maroon%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(width%3D0.5)%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Then%20we%20can%20add%20new%20features%20to%20our%20plot%20by%20using%20Plotly's%20function%3A%20update_layout%0A%20%20%20%20pic.update_layout(%0A%20%20%20%20%20%20%20%20margin%3Ddict(l%3D70%2C%20r%3D60%2C%20t%3D70%2C%20b%3D60)%2C%0A%20%20%20%20%20%20%20%20title_text%3D%22My%20fancy%20title%22%2C%0A%20%20%20%20%20%20%20%20title_x%3D0.5%2C%0A%20%20%20%20%20%20%20%20height%3D500%2C%0A%20%20%20%20%20%20%20%20width%3D980%2C%0A%20%20%20%20%20%20%20%20hovermode%3D%22x%20unified%22%2C%0A%20%20%20%20%20%20%20%20xaxis%3Ddict(%0A%20%20%20%20%20%20%20%20%20%20%20%20title%3D%22Quarterly%20observations%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20range%3D%5B%221959-04-01%22%2C%20%222024-10-01%22%5D%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20yaxis%3Ddict(title%3D%22Percentage%20points%22%2C%20range%3D%5B-2%2C%2018%5D)%2C%0A%20%20%20%20%20%20%20%20font_family%3D%22Calibri%22%2C%0A%20%20%20%20%20%20%20%20font_size%3D15%2C%0A%20%20%20%20)%0A%20%20%20%20pic%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%206.%20Auxiliary%20files%20(do%20not%20remove%20them)%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(pd)%3A%0A%20%20%20%20%23%20Making%20data%20frames%20looking%20like%20in%20Julia%20and%20Pluto%20(first%205%20rows%20%2B%20last%20row)%0A%20%20%20%20def%20preview(df%2C%20n%3D5)%3A%0A%20%20%20%20%20%20%20%20if%20len(df)%20%3C%3D%20n%20%2B%201%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20df%0A%0A%20%20%20%20%20%20%20%20ellipsis%20%3D%20pd.DataFrame(%0A%20%20%20%20%20%20%20%20%20%20%20%20%5B%5B%22...%22%5D%20*%20df.shape%5B1%5D%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20columns%3Ddf.columns%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20index%3D%5B%22%E2%80%A6%22%5D%2C%0A%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20%20%20%20%20return%20pd.concat(%0A%20%20%20%20%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20df.head(n)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20ellipsis%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20df.tail(1)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20return%20(preview%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20%23%20Global%20CSS%20for%20kbd%20elements%20throughout%20the%20notebook%0A%20%20%20%20mo.Html(%22%22%22%0A%20%20%20%20%20%20%20%20%3Cstyle%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20kbd%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20background-color%3A%20%23505050%20!important%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20color%3A%20%23fff%20!important%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20padding%3A%203px%206px%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20border-radius%3A%204px%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20font-family%3A%20monospace%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20font-size%3A%200.9em%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20border%3A%200px%20solid%20%23666%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%3C%2Fstyle%3E%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20return%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
538c3fa1610f58f54f69a786b8505e65